function Snake(el,t) {
    this.el = el//保存最大地图对象
    //保存小蛇一节的长度
    this.len = 20
    //保存蛇
    this.snakeObj = [
        { x: 3, y: 2, color: 'skyblue', ele: null }//蛇头
        , { x: 2, y: 2, color: 'springgreen', ele: null }//蛇身体第一节
        , { x: 1, y: 2, color: 'gray', ele: null }]//蛇身体第二节
    this.direction = 'right'
    this.t=t
}
Snake.prototype.create = function () {
    // 清除地图上的蛇
    this.remove()
    for (let i = 0; i < this.snakeObj.length; i++) {
        let divObj = document.createElement('div')
        //设置宽度高度 坐标和颜色
        divObj.style.width = this.len + 'px'
        divObj.style.height = this.len + 'px'
        divObj.style.position = 'absolute'
        divObj.style.left = this.snakeObj[i].x * this.len + 'px'
        divObj.style.top = this.snakeObj[i].y * this.len + 'px'
        divObj.style.background = this.snakeObj[i].color
        // 单独保存一下每一节蛇的对象
        this.snakeObj[i].ele = divObj
        //将小蛇追加到页面显示
        this.el.appendChild(divObj)
    }
}
// 移出蛇节点
Snake.prototype.remove = function () {
    for (let i = 0; i < this.snakeObj.length; i++) {
        //判断是否创建 创建了才移除蛇
        if (this.snakeObj[i].ele) {
            this.el.removeChild(this.snakeObj[i].ele)
        }
    }
}
// 让蛇移动
Snake.prototype.move = function () {
    //从蛇尾开始 跟着前一个块
    for (let i = this.snakeObj.length - 1; i > 0; i--) {
        this.snakeObj[i].x = this.snakeObj[i - 1].x
        this.snakeObj[i].y = this.snakeObj[i - 1].y
    }
    //控制蛇头移动方向
    switch (this.direction) {
        case 'top':
            this.snakeObj[0].y -= 1
            break;
        case 'bottom':
            this.snakeObj[0].y += 1
            break;
        case 'left':
            this.snakeObj[0].x -= 1
            break;
        case 'right':
            this.snakeObj[0].x += 1
            break;
    }
    //修改了x的值 然后在重新创建蛇
    this.create()
}